//
//  JLNewFeatureViewController.swift
//  新浪微博
//
//  Created by linlin on 15/11/25.
//  Copyright © 2015年 jilin. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class JLNewFeatureViewController: UICollectionViewController {

    
   //页面数
    let itemCount = 4
    
   
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //layout
    private let layout = UICollectionViewFlowLayout()
    
    //重写构造方法
    init() {
        //设置为流水
        super.init(collectionViewLayout: layout)
        
    }
    

    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //注册一个cell
        collectionView!.registerClass(JLNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        //设置layout
        prepareLayout()
        
        collectionView?.backgroundColor = UIColor.whiteColor()
        
    }

    //设置layout
    private func prepareLayout() {
        //item大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        
        //滚动方向:水平
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //分页
        collectionView?.pagingEnabled = true
        
        //取消弹簧效果
        collectionView?.bounces = false
    }
    
    
    
    
    ///数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! JLNewFeatureCell
        
        cell.index = indexPath.item
        return cell
    
    }
    
    
    //如果collectionView停止下来，展示一个cell的时候调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //过去显示的cell indexPath
        let index = collectionView.indexPathsForVisibleItems().last!
        
        if index.item == itemCount - 1 {
            //开始动画
            let cell = collectionView.cellForItemAtIndexPath(index) as! JLNewFeatureCell
            
            cell.startBtnAnimation()
        }
    }
    
    
    
}




class JLNewFeatureCell: UICollectionViewCell {
    
    //计算是第几个cell
    var index: Int = 0 {
        didSet{
        //根据index加载图片
        self.bgImageView.image = UIImage(named: "new_feature_\(index + 1)")
          
        self.statrButton.hidden = true
            
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    
    //按钮动画
    private func startBtnAnimation() {
        statrButton.hidden = false
        
        statrButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.0, delay: 0.1, usingSpringWithDamping: 5, initialSpringVelocity:0.5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.statrButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                print("动画结束")
        }
        
    }
    

    
    
    
    
    //准备UI
    private func prepareUI() {
        contentView.addSubview(bgImageView)
        contentView.addSubview(statrButton)
        
        
        bgImageView.translatesAutoresizingMaskIntoConstraints = false
        statrButton.translatesAutoresizingMaskIntoConstraints = false
        
        
        //添加约束
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView" : bgImageView]))
      
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subView" : bgImageView]))

        
        contentView.addConstraint(NSLayoutConstraint(item: statrButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: statrButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -150))
        

        
        
        
        
    }
    
    //跳转到main界面
     func loadMainView () {
        
        AppDelegate.swichViewRootController(true)
    }
    
    
    
    
    //懒加载
    private lazy var bgImageView: UIImageView = {
        var view = UIImageView()
        view.image = UIImage(named: "new_feature_1")
        return view
    }()
    
    private lazy var statrButton: UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        
        btn.sizeToFit()
        
        btn.addTarget(self, action: "loadMainView", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
    
}




